//
//  KDWebBlockEditController.m
//  PublicSentimentMonitorSystem
//
//  Created by 魏强 on 15/11/9.
//  Copyright © 2015年 魏强. All rights reserved.
//

#import "KDWebBlockEditController.h"
#import "KDWebSetCellModel.h"
#import "KDMonitorRequestTool.h"
#import "AddWebBlockParam.h"
#import "AddWebBlockResult.h"
#import "KDMSettingCellModel.h"
#import "KDWebEditCellModel.h"
#import "SVProgressHUD.h"
#import "findWebBloclListResult.h"
@interface KDWebBlockEditController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *nameTf;
@property (weak, nonatomic) IBOutlet UITextField *addressTf;
@property (weak, nonatomic) IBOutlet UIButton *addBtn;

@end

@implementation KDWebBlockEditController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpNavBar];
    self.nameTf.delegate = self;
    self.addressTf.delegate = self;
    _addBtn.backgroundColor = KDHeadColor;
    _addBtn.layer.cornerRadius = 5;
    UITapGestureRecognizer *hide = [[UITapGestureRecognizer alloc] init];
    [hide addTarget:self action:@selector(hideTheKeyBoard)];
    [self.view addGestureRecognizer:hide];
    // Do any additional setup after loading the view from its nib.
}

- (void)setUpNavBar{
    self.navigationController.navigationBar.hidden = NO;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"leftArrow"] style:UIBarButtonItemStylePlain target:self action:@selector(backViewClick)];
}

#pragma mark - 网络请求区
- (void)addBlockRequest{
    AddWebBlockParam *param = [[AddWebBlockParam alloc]init];
    param.wsm_id = [self.settingCellModel.m_Id integerValue];
    param.name = self.nameTf.text;
    param.url = self.addressTf.text;
    if (!self.nameTf.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入板块名称！" maskType:SVProgressHUDMaskTypeGradient ];
        return;
    }else if (!self.addressTf.text.length){
        [SVProgressHUD showErrorWithStatus:@"请输入板块网址！" maskType:SVProgressHUDMaskTypeGradient ];
        return;
    }
    param.method = @"addMonitorWebSiteModule";
    param.wsId = self.settingCellModel.ws_id;
    [SVProgressHUD showWithStatus:@"正在添加！" maskType:SVProgressHUDMaskTypeGradient ];
    [KDMonitorRequestTool addWebBlockMonitorWithParam:param success:^(AddWebBlockResult *responseObejct) {
        if ([responseObejct.status isEqualToString:@"ok"]) {
            if (responseObejct.info  == -1 ) {//表示该主题已经存在了
                [SVProgressHUD showInfoWithStatus:@"该板块已存在！" maskType:SVProgressHUDMaskTypeGradient];
            }else if (responseObejct.info  == 1 ){
                [SVProgressHUD showInfoWithStatus:@"板块添加成功！" maskType:SVProgressHUDMaskTypeGradient];
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [SVProgressHUD showInfoWithStatus:@"板块添加失败！" maskType:SVProgressHUDMaskTypeGradient];
            }
        }
        
        
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"未知网络错误！" maskType:SVProgressHUDMaskTypeGradient];
        KDLog(@"%@",error);
    }];
}

#pragma mark - 事件点击区
- (IBAction)addBtnClick:(id)sender {
    //如果是真数据的话那么就要做判断，判断该网址是否属于子板块
    [self addBlockRequest];
}

/**
 *  返回按钮被点击了
 */
- (void)backViewClick{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 隐藏键盘
- (void)hideTheKeyBoard
{
    [self.view endEditing:YES];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
